Want something to MOVE? You want tween_to or tween_from_to, not this: they start right now and move a variable for you. This one only works out a number for you to use yourself - it glides from `from` to `to` over `duration` frames, starting at frame `start_frame`, speeding up or braking the way the curve says - a bar that fills smoothly, a menu that slides in, a coin that floats up. Before it starts the answer is `from`; once the time is up it is `to`, and stays `to`.
Works differently on Game Boy - The Game Boy works out the curve in 32 steps, so a very long glide over a long way moves in small jumps.
Works differently on Game Boy Color - The Game Boy Color works out the curve in 32 steps, so a very long glide over a long way moves in small jumps.
On other consoles this differs: see Side by side and How the consoles differ.
tween(from, to, start_frame, duration, ease) -> real| Name | Type |
|---|---|
from | real |
to | real |
start_frame | int |
duration | int |
ease | easing |
real
Do you just want something to move? Then you want tween_to or tween_from_to, not this page. They start right now and move the variable for you. tween is for when you want to work out the number yourself, to draw it or compare it.
A number that glides from from to to. You say when it starts (start_frame), how long it takes (duration, in frames) and what the glide feels like (ease, one of the sixteen easing curves). Ask for it again every frame and it gives you the next number along.
It keeps nothing of its own. It works out where the glide should be from the game's frame counter, the same one wave reads. Before start_frame the answer is from. Once start_frame + duration has passed the answer is to, and it stays to for ever.
y = tween(0, 80, 60, 120, EASE_QUAD_OUT);Until frame 60 the walker's y is 0. From then it slides down to 80 over the next 120 frames (two seconds), fast at first and braking as it arrives. From frame 180 on it is 80, every frame, with nothing more to do.
A glide usually starts when something happens, not at frame 60. So keep the moment in a variable. Write it down once (start = game_frame(); in the Create event, or whenever the thing begins), then use it every frame:
x = tween(-20, 120, start, 90, EASE_SINE_OUT);Here x slides in from -20 to 120 over a second and a half, starting at whatever frame start was written down. To play it again, write start = game_frame(); again.
start_frame and duration count frames: 60 is about one second. If you think in seconds, multiply once where you write it: tween(0, 80, start, 2 * 60, EASE_LINEAR).
Both are whole numbers (int), not numbers with a point. That is on purpose. The frame counter gets bigger than the biggest number a real can hold after about two and a half hours of play. tween counts the frames in whole numbers all the way, so it is as exact on hour five as on minute one. Give it a number with a point and the build will tell you to use a whole one.
How far along the glide is, after a quarter, half and three quarters of its time:
| Curve | A quarter | Half | Three quarters |
|---|---|---|---|
EASE_LINEAR | 0.25 | 0.50 | 0.75 |
EASE_SINE_IN | 0.08 | 0.29 | 0.62 |
EASE_SINE_OUT | 0.38 | 0.71 | 0.92 |
EASE_SINE_IN_OUT | 0.15 | 0.50 | 0.85 |
EASE_QUAD_IN | 0.06 | 0.25 | 0.56 |
EASE_QUAD_OUT | 0.44 | 0.75 | 0.94 |
EASE_QUAD_IN_OUT | 0.13 | 0.50 | 0.88 |
EASE_BACK_IN | -0.06 | -0.09 | 0.18 |
EASE_BACK_OUT | 0.82 | 1.09 | 1.06 |
EASE_BACK_IN_OUT | -0.10 | 0.50 | 1.10 |
EASE_BOUNCE_IN | 0.03 | 0.23 | 0.53 |
EASE_BOUNCE_OUT | 0.47 | 0.77 | 0.97 |
EASE_BOUNCE_IN_OUT | 0.12 | 0.50 | 0.88 |
EASE_ELASTIC_IN | -0.01 | -0.02 | 0.09 |
EASE_ELASTIC_OUT | 0.91 | 1.02 | 1.01 |
EASE_ELASTIC_IN_OUT | 0.01 | 0.50 | 0.99 |
An "in" curve starts slowly and speeds up. An "out" curve starts fast and brakes. An "in out" curve does both. Sine is the gentle one and quad is the stronger one. Back, bounce and elastic are the lively ones: they overshoot, bounce and wobble.
A number above 1.00 in the table means the glide has gone past to at that moment, and a number below 0.00 means it is behind from. Only back and elastic ever do that, and the table is where you can see it: EASE_BACK_OUT is already at 1.09 after half its time.
toIt is easy to think a curve that goes past to cannot stay at to afterwards. It can, and these do: after the end the answer is exactly to, every frame, for every curve. That promise is about what happens after the end, and swinging past to on the way there does not touch it.
What back and elastic do change is something the first seven curves never did: on the way, the number is not always between from and to. A bounce is different again: it never leaves the range at all.
Goes past to | Goes behind from | Exactly to at the end | |
|---|---|---|---|
| Bounce | never | never | yes |
| Back | about 10% (out, in out) | about 10% (in, in out) | yes |
| Elastic | about 37% (out), about 12% (in out) | about 37% (in), about 12% (in out) | yes |
A bounce is a ball dropped on to: it falls to it and bounces back up from it, each bounce a quarter as tall as the one before, so it touches to but never goes past it. Back and elastic go past on purpose, because that is what makes a move feel springy. So give them to a position, a size or a color, and do not give them to a number that must stay inside limits, like health or a volume: a health that is tweened with EASE_ELASTIC_OUT swings past full. Each curve's own page says how far it goes and how to pick a safer one.
Sixteen curves are enough to make almost anything feel right. These are left out on purpose:
sqrt is only exact to 1/64. A curve made from it would climb in small visible steps exactly where it is steepest.If you want a spring of your own size, a spring is a wave that dies away, which you can write yourself:
// a spring: swings 20 either side, and each swing is smaller than the one before
swing = wave(-20, 20, 30, 0) * (1 - tween(0, 1, start, 120, EASE_LINEAR));tween reads game_frame, which keeps counting while the game is paused. A glide you read with tween carries on through a pause, so if the pause is long, it will already be finished when the game comes back. tween_to, the form that moves a variable for you, waits while the game is paused instead. Pick the one that matches what you want.
A handful of whole-number sums, and for a sine curve one sine look-up. An elastic curve does a little more: a sine and a fading factor (a few small multiplications) on every frame it is read. Nothing is kept anywhere, and a game pays only where it calls it. Reading it does not put the tween manager (the part that moves variables for you) in your game.
The time is the same always-advancing frame counter wave() reads (game_frame(), see its page), so it keeps no timer of its own: you note the frame it should start at (`start = game_frame();`) and ask for the value again every frame. start_frame and duration are whole numbers (int), not reals, on purpose: the frame counter passes the biggest number a real can hold after about 2.4 hours of play, and wave() once had that very bug. Here the frames are counted in whole numbers all the way, and only the 0-to-1 progress is turned into a fraction, so it is exact for as long as the console is on. duration is in frames (60 is about a second); a duration of 0 or less has no time to cross, so the answer is `to` straight away. The blend is exact to one 1/4096 step. Sixteen curves: EASE_LINEAR, and sine, quad, back, bounce and elastic, each as _IN, _OUT and _IN_OUT. Most stay between `from` and `to`, and so does every EASE_BOUNCE_ curve (a ball dropped on the end value never goes past it), but the EASE_BACK_ and EASE_ELASTIC_ curves go PAST them on purpose, for a moment: about 10% for back, over a third of the distance for an elastic out. A number tweened with one of those leaves the range from `from` to `to`, so do not use them on something that must stay in a range, like health or a volume. Every curve lands exactly on `to` at the end and stays there. Cubic and steeper are left out on purpose, so are circ (it needs a square root and this engine’s sqrt is only exact to 1/64) and expo. This reading form follows game_frame(), which keeps counting while the game is paused; tween_to, the form that moves a variable for you, waits while the game is paused.